﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Domain.Models;
using Domain.DAL;
using Utilities;
using Casanova.Web;

namespace WebApp.RepairQualities
{
    public partial class RepairQualityEdit : System.Web.UI.Page
    {
        #region Private Properties

        public int RepairQualitiesID
        {
            get
            {
                if (this.ViewState["ID"] != null)
                {
                    return Convert.ToInt32(this.ViewState["ID"]);
                }
                return MyConfig.NullNumber;
            }
            set
            {
                this.ViewState["ID"] = value.ToString();
            }
        }

        #endregion

        #region Event Handlers

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                (Master as SiteMaster).SelectedTab = "systemAdministrator";
                if (!IsPostBack)
                {
                    this.pageTitle.Text = Resources.Strings.SystemAdministrator + @"\" + Resources.Strings.RepairQualityManagement;
                    GetParam();
                    LoadFormForEdit();
                }
            }
            catch (Exception ex)
            {
                Utility.ShowMessageBox(this.Page, ex.ToString());
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.RepairQualitiesID == MyConfig.NullNumber) //Insert
                {
                    if (InsertRepairQuality())
                    {
                        //Utility.ShowMessageBox(this.Page, Resources.Strings.SaveSuccess);
                    }
                    else
                    {
                        Utility.ShowMessageBox(this.Page, Resources.Strings.SaveFail);
                        return;
                    }
                }
                else     //Update
                {
                    if (UpdateRepairQuality())
                    {
                        //Utility.ShowMessageBox(this.Page, Resources.Strings.UpdateSuccess);
                    }
                    else
                    {
                        Utility.ShowMessageBox(this.Page, Resources.Strings.UpdateFail);
                        return;
                    }
                }
                Response.Redirect(this.ResolveUrl("~/RepairQualities/RepairQualityManagement.aspx"));
            }
            catch (Exception ex)
            {
                Utility.ShowMessageBox(this.Page, ex.ToString());
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect(this.ResolveUrl("~/RepairQualities/RepairQualityManagement.aspx"));
        }

        #endregion

        #region Private Methods
        private bool CheckValidate()
        {
            if (string.IsNullOrEmpty(txtRepairQualityName.Text.Trim()))
            {
                return false;
            }
            return true;
        }

        private bool InsertRepairQuality()
        {
            bool result = false;
            try
            {
                if (!CheckValidate())
                {
                    result = false;
                }
                else
                {
                    RepairQualityDAO rDAO = RepairQualityDAO.GetInstance();
                    RepairQuality r = GetRepairQualityInfo();
                    //Insert Employee.
                    int temp = rDAO.Insert(r);
                    if (temp != MyConfig.NullNumber)
                    {
                        this.RepairQualitiesID = temp;
                        result = true;
                    }
                }
            }
            catch (Exception)
            {
                result = false;
                throw;
            }
            return result;
        }

        private bool UpdateRepairQuality()
        {
            bool result = false;
            try
            {
                if (!CheckValidate())
                {
                    result = false;
                }
                RepairQualityDAO rDAO = RepairQualityDAO.GetInstance();
                RepairQuality r = GetRepairQualityInfo();
                r.ID = RepairQualitiesID;
                //Update Employee.
                result = rDAO.Update(r);
            }
            catch (Exception)
            {
                result = false;
                throw;
            }
            return result;
        }

        private RepairQuality GetRepairQualityInfo()
        {
            RepairQuality r = new RepairQuality();
            try
            {
                r.RepairQualityName = txtRepairQualityName.Text.Trim();
            }
            catch (Exception)
            {
                r = null;
                throw;
            }
            return r;
        }

        private void GetParam()
        {
            this.RepairQualitiesID = QueryStringUtility.GetInt32("ID", MyConfig.NullNumber);
        }

        private void LoadFormForEdit()
        {
            try
            {
                RepairQualityDAO rDAO = RepairQualityDAO.GetInstance();
                RepairQuality r = new RepairQuality();
                r = rDAO.Get(this.RepairQualitiesID);

                if (r != null)
                {
                    txtRepairQualityName.Text = r.RepairQualityName;
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        #endregion
    }
}